import typescript from 'rollup-plugin-typescript2';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import path from 'path';
import vue from 'rollup-plugin-vue';
import { terser } from 'rollup-plugin-terser';
import { getPackagesSync } from '@lerna/project';
import pkg from '../package.json';
const deps = Object.keys( pkg.devDependencies );
const inputs = getPackagesSync().map( pck => pck.name ).filter( name => name.includes( '@fow-code-ui' ) );
/**
 * es 组件单个打包
 */
export default inputs.map( name => {
  const pckName = name.split( '@fow-code-ui' )[ 1 ]
  return {
    input: path.resolve( __dirname, `../packages/${ pckName }/index.ts` ),
    output: {
      format: 'es',
      file: `lib/${ pckName }/index.js`,
    },
    plugins: [
      terser(), // 压缩js代码，包括es6代码压缩
      nodeResolve(), // 自动找index文件
      vue( {
        target: 'browser',
        css: false,
        exposeFilename: false,
      } ),
      typescript( { // 默认调用tsconfig.json  帮我们生成声明文件
        tsconfigOverride: {
          compilerOptions: { // 打包单个组件的时候不生成ts声明文件
            declaration: false,
          },
          include: [ // 包含
            'packages/**/*',
            'typings/vue-shim.d.ts',
          ],
          exclude: [ // 排除
            'node_modules',
            'packages/**/__tests__/*',
            'website'
          ]
        },
        abortOnError: false,
      } )
    ],
    external ( id ) { // 排除
      return /^vue/.test( id )
        || deps.some( k => new RegExp( '^' + k ).test( id ) )
        || /^@fow-code-ui/.test( id )
    },
  }

} )

